package com.cash.channel.svc.service;

import com.aliyuncs.utils.StringUtils;
import com.cash.channel.svc.logic.ChannelLogic;
import com.cash.channel.svc.model.ChannelInfo;
import com.alibaba.fastjson.JSON;
import com.cash.channel.api.exception.CashChannelException;
import com.cash.channel.api.model.ApiRequestModel;
import com.cash.channel.api.model.ApiResponseModel;
import com.cash.channel.api.model.channel.ChannelModel;
import com.cash.channel.api.service.ApiService;
import com.cash.channel.api.service.BaseApi;
import com.cash.channel.svc.util.ServiceUtils;
import com.cash.common.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @blame taihao
 */
@Slf4j
@Service("apiService")
public class ApiServiceImpl implements ApiService {

    @Autowired
    private ChannelLogic channelLogic;

    @Override
    @SuppressWarnings("unchecked")
    public ApiResponseModel api(ApiRequestModel apiRequestModel) {
        try {
            log.info(String.format("@@@API接口入参channel=%s|key=%s|param=%s", apiRequestModel.getChannel(), apiRequestModel.getKey(), apiRequestModel.getData()));
            BaseApi baseApi = ServiceUtils.getApiService(apiRequestModel.getKey());
            ApiResponseModel apiResponseModel = baseApi.doProcess(apiRequestModel);
            log.info(String.format("@@@API接口出参result=%s", JSON.toJSONString(apiResponseModel)));
            return apiResponseModel;
        } catch (CashChannelException e) {
            log.error(String.format("@@@API接口失败result=%s", e.getErrMessage()), e);
            return new ApiResponseModel<>(false, -1, e.getErrMessage(), null);
        }
    }

    @Override
    public Result<ChannelModel> getChannelInfoByCode(String channelCode) throws CashChannelException {
        try {
            if (StringUtils.isEmpty(channelCode)) {
                return Result.failed("渠道号不能为空");
            }
            ChannelModel channelModel = new ChannelModel();
            ChannelInfo channelInfo = channelLogic.getChannelInfoByCode(channelCode);
            BeanUtils.copyProperties(channelInfo, channelModel);
            return Result.succeed(channelModel);
        } catch (CashChannelException e) {
            log.error(e.getErrMessage(), e);
            return Result.failed(e.getErrMessage());
        }
    }

    @Override
    public Result<ChannelModel> getChannelInfoByChannelId(String channel) {
        try {
            if (StringUtils.isEmpty(channel)) {
                return Result.failed("渠道号不能为空");
            }
            ChannelModel channelModel = new ChannelModel();
            ChannelInfo channelInfo = channelLogic.getChannelInfoByChannelId(channel);
            BeanUtils.copyProperties(channelInfo, channelModel);
            return Result.succeed(channelModel);
        } catch (CashChannelException e) {
            log.error(e.getErrMessage(), e);
            return Result.failed(e.getErrMessage());
        }
    }

}
